var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (t) {
  return typeof t;
} : function (t) {
  return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
},
    o = "function" == typeof Symbol && "symbol" == t(Symbol.iterator) ? function (o) {
  return void 0 === o ? "undefined" : t(o);
} : function (o) {
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : void 0 === o ? "undefined" : t(o);
},
    e = function (t) {
  return t && t.__esModule ? t : {
    default: t
  };
}(require("siteinfo.js")),
    n = ["pages/index/index", "pages/category/index", "pages/flow/index", "pages/user/index", "pages/find/find"];

require("./utils/ald-stat.js");

App({
  globalData: {
    user_id: 7001,
    appid: "wx748887e105fcb479"
  },
  api_root: "",
  onLaunch: function () {
    tt.setStorageSync("token", "ed6274fc2c79643a89e3f38fb141f3e8"), tt.setStorageSync("user_id", 7001), this.setApiRoot(), this.getWxappBase();
  },
  onShow: function (t) {},
  setApiRoot: function () {
    this.api_root = e.default.siteroot + "index.php?s=/api/";
  },
  getWxappBase: function (t) {
    this._get("wxapp/base", {}, function (o) {
      console.log(o.token), tt.setStorageSync("token", o.token), tt.setStorageSync("user_id", o.user_id), tt.setStorageSync("wxapp", o.data.wxapp), t && t(o.data.wxapp);
    }, !1, !1);
  },
  doLogin: function () {
    var t = getCurrentPages();

    if (t.length) {
      var o = t[t.length - 1];
      "pages/login/login" != o.route && tt.setStorageSync("currentPage", o);
    }

    tt.navigateTo({
      url: "/pages/login/login"
    });
  },
  getUserId: function () {
    return tt.getStorageSync("user_id") || 0;
  },
  showSuccess: function (t, o) {
    tt.showToast({
      title: t,
      icon: "success",
      success: function () {
        o && setTimeout(function () {
          o();
        }, 1500);
      }
    });
  },
  showError: function (t, o) {
    // tt.showModal({
    //   title: "友情提示",
    //   content: t,
    //   showCancel: !1,
    //   success: function (t) {
    //     o && o();
    //   }
    // });
  },
  _get: function (t, e, n, a, i, r) {
    var s = this;
    tt.showNavigationBarLoading(), e = Object.assign({
      wxapp_id: 10001,
      token: tt.getStorageSync("token"),
      appid: s.globalData.appid
    }, e);

    var c = function () {
      e.token = tt.getStorageSync("token"), tt.request({
        url: s.api_root + t,
        header: {
          "content-type": "application/json"
        },
        data: e,
        success: function (t) {
          if (200 !== t.statusCode || "object" !== o(t.data)) return console.log(t); // s.showError("网络请求出错"), 

          !1;
          if (-1 === t.data.code) tt.hideNavigationBarLoading(), s.doLogin();else {
            if (0 === t.data.code) return s.showError(t.data.msg), !1;
            n && n(t.data);
          }
        },
        fail: function (t) {
          s.showError(t.errMsg, function () {
            a && a(t);
          });
        },
        complete: function (t) {
          tt.hideNavigationBarLoading(), i && i(t);
        }
      });
    };

    r ? s.doLogin(c) : c();
  },
  _post_form: function (t, e, n, a, i) {
    tt.showNavigationBarLoading();
    var r = this;
    e = Object.assign({
      wxapp_id: 10001,
      token: tt.getStorageSync("token"),
      appid: r.globalData.appid
    }, e), tt.request({
      url: r.api_root + t,
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: e,
      success: function (i) {
        return 200 !== i.statusCode || "object" !== o(i.data) ? (console.log("网络请求出错"), !1) : -1 === i.data.code ? (r.doLogin(function () {
          r._post_form(t, e, n, a);
        }), !1) : 0 === i.data.code ? (r.showError(i.data.msg, function () {
          a && a(i);
        }), !1) : void (n && n(i.data));
      },
      fail: function (t) {
        r.showError(t.errMsg, function () {
          a && a(t);
        });
      },
      complete: function (t) {
        tt.hideLoading(), tt.hideNavigationBarLoading(), i && i(t);
      }
    });
  },
  validateUserInfo: function () {
    return tt.getStorageSync("user_info"), !!tt.getStorageSync("user_info");
  },
  urlEncode: function (t) {
    var o = [];

    for (var e in t) {
      var n = t[e];
      n.constructor == Array ? n.forEach(function (t) {
        o.push(e + "=" + t);
      }) : o.push(e + "=" + n);
    }

    return o.join("&");
  },
  setTitle: function () {// var t = this, o = void 0;
    // (o = wx.getStorageSync("wxapp")) ? wx.setNavigationBarTitle({
    //     title: o.navbar.wxapp_title
    // }) : t.getWxappBase(function() {
    //     t.setTitle();
    // });
  },
  setNavigationBar: function () {
    this.getWxappBase(function (t) {
      tt.setNavigationBarColor({
        frontColor: t.navbar.top_text_color.text,
        backgroundColor: t.navbar.top_background_color
      });
    });
  },
  getTabBarLinks: function () {
    return n;
  },
  checkIsLogin: function () {
    return "" != tt.getStorageSync("token") && "" != tt.getStorageSync("user_id");
  },
  getUserInfo: function (t, o) {
    var e = this;
    if ("getUserInfo:ok" !== t.detail.errMsg) return !1;
    tt.showLoading({
      title: "正在登录",
      mask: !0
    }), tt.login({
      success: function (n) {
        e._post_form("user/login", {
          code: n.code,
          user_info: t.detail.rawData,
          encrypted_data: t.detail.encryptedData,
          iv: t.detail.iv,
          signature: t.detail.signature
        }, function (t) {
          tt.setStorageSync("token", t.data.token), tt.setStorageSync("user_id", t.data.user_id), o && o();
        }, !1, function () {
          tt.hideLoading();
        });
      }
    });
  }
});